package cn.edu.jxust.controller.api.account;

import cn.edu.jxust.common.domain.dto.AccountRoleVo;
import cn.edu.jxust.common.domain.dto.AccountVo;
import cn.edu.jxust.common.domain.dto.PasswordVo;
import cn.edu.jxust.common.domain.dto.SlfInfoVo;
import cn.edu.jxust.common.domain.web.api.account.IAccountController;
import cn.edu.jxust.common.response.Result;
import cn.edu.jxust.common.response.ResultUtils;
import cn.edu.jxust.common.response.enums.ResultStatus;
import cn.edu.jxust.core.domain.repository.AccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author QiuKangming
 * @version 1.0
 * @since 2020/07/04 09:37
 */

@Slf4j
@RestController

public class AccountController implements IAccountController {

    @Resource
    private AccountService accountService;

    @Override
    public Result addAccount(AccountVo accountVo, BindingResult result){

        if(result.hasErrors()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            return accountService.addAccountInfo(accountVo);
        }catch (Exception e){
            log.error("[AccountController --> addAccount 异常]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result delAccount(Integer userId){

        try{
            return accountService.delAccountById(userId);
        }catch (Exception e){
            log.error("[AccountController --> delAccount 异常]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result delAccountBatch(String userIdsStr){

        try {
            return accountService.delAccountBatch(userIdsStr);
        }catch (Exception e){
            log.error("[AccountController --> delAccountBatch 异常]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result modifyAccount(AccountVo accountVo, BindingResult result){

        if(result.hasErrors() || accountVo.getId() == null){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            return accountService.modifyAccount(accountVo)
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS, "账户修改成功")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED, "账户修改失败, 请重试!");
        }catch (Exception e){
            log.error("[AccountController --> modifyAccount 异常]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result findAccountListOrQuality(Integer page, Integer size, HttpServletRequest request){

        try {
            //获取客户端传过来的数组, 需要排除 page 和 size
            Map<String, String[]> paramMap = new HashMap<>(request.getParameterMap());
            return accountService.getAccountVoList(paramMap, page, size);
        }catch (Exception e){
            log.error("[AccountController --> findAccountListOrQuality 异常]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result toAccountRole(AccountRoleVo accountRoleVo, BindingResult result){

        if(result.hasErrors()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }

        try{
            return accountService.toAccountRole(accountRoleVo);
        }catch (Exception e){
            log.error("[AccountController --> toAccountRole 异常]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result resetPwd(String username){

        try{
            if(StringUtils.isEmpty(username)){
                return ResultUtils.resultWithoutData(ResultStatus.FAILED, "请携带正确信息");
            }
            return accountService.resetPwd(username);
        }catch (Exception e){
            log.error("[AccountController --> resetPwd 异常]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result modifySelf(SlfInfoVo slfInfo, BindingResult result){

        if(result.hasErrors()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            return accountService.modifySelfInfo(slfInfo);
        }catch (Exception e){
            log.error("[AccountController --> modifySelf 异常]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result modifySelfPwd(PasswordVo passwordVo, BindingResult result){

        if(result.hasErrors()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }

        try {
            return accountService.modifySelfPwd(passwordVo);
        }catch (Exception e){
            log.error("[AccountController --> modifySelfPwd 异常]: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }
}
